using System;
using System.Collections.Generic;
using System.Text;

using Google.GData.Client;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;

namespace puyan_blogger {
    class PloggerAtomUtil {

        // FUCTIONALITY: post content to blog.
        public static void post_content_to_blog(BlogPost_cntr blogpost, 
            BlogQuery_cntr blogquery, Uri blogUri, bool isDraft) {
            AtomEntry newPost = new AtomEntry();
            newPost.Title.Text = blogpost.Title;
            newPost.Content = new AtomContent();
            newPost.Content.Content = "<div xmlns='http://www.w3.org/1999/xhtml'>" +
                blogpost.Content +
                "</div>";
            newPost.Content.Type = "xhtml";
            newPost.Authors.Add(new AtomPerson());
            newPost.Authors[0].Name = "Dyson Sphere"; //blogpost.Userinfo.Fullname;
            newPost.Authors[0].Email = "asdf@gmail.com"; //blogpost.Userinfo.EmailAddress;
            newPost.IsDraft = isDraft;
            /*char[] spitTagsChar = { ',' };
            foreach (String tag in tagNewCategoryTextBox.Text.Split(spitTagsChar)){
                AtomCategory atomcat = new AtomCategory();
                atomcat.Term = tag;
                newPost.Categories.Add(atomcat);}*/
            AtomEntry createdEntry = blogquery.AtomService.Insert(blogUri, newPost);
        }

        // FUNCTIONALITY: display atom feed
        private static string contruct_html_doc_from_atom_feed(AtomFeed atomfeed) {
            string blogcontent = "<html> <head>" + "<title>" + "Puyan Blogger" +
                "</title></head><body><font size=1 face=\"Verdana, Arial, Sans\">" +
                "<hr/><h1>" + atomfeed.Title.Text + "</h1><hr/>";
            foreach (AtomEntry entry in atomfeed.Entries) {
                blogcontent = blogcontent + "<br/><br/>" + "<br/><br/>" +
                    "<strong>" + "<a href=\"" + entry.EditUri + "\">" + entry.Title.Text + ": </a> </strong>" +
                    "<br/>" + "<br/>" + entry.Content.Content +
                    "<br/><br/><br/>" + "<strong> Tags: </strong>";
                foreach (AtomCategory cat in entry.Categories) {
                    blogcontent = blogcontent + cat.Term;
                    if (cat != entry.Categories[entry.Categories.Count - 1]) {
                        blogcontent = blogcontent + ", ";
                    }
                }
                blogcontent = blogcontent + "<br/>" + "<br/>  <hr /> ";
            }
            return (blogcontent + "<br/><br/>" + "Atom Feed!" + "</font></body></html>");
        }

        public static void refresh_rss(System.Windows.Forms.WebBrowser blogReaderWebBrowser,
                                 BlogQuery_cntr blog_service_query_info,
                                 Uri blogUri) {

            if (blogReaderWebBrowser == null || blog_service_query_info == null || blogUri == null) {
                return;
            }

            Google.GData.Client.Service atomService = blog_service_query_info.AtomService;
            Google.GData.Client.FeedQuery atomQuery = blog_service_query_info.AtomQuery;


            atomQuery.Uri = blogUri;
            AtomFeed feed = atomService.Query(atomQuery); // TODO: catch bad credentials...


            System.Console.WriteLine(feed.Authors.Count);
            //tagList.ForeColor = Color.Black; TODO: fix this in login
            

            /*foreach (AtomEntry entry in feed.Entries) {
                foreach (AtomCategory cat in entry.Categories) {
                    bool already_taged = false;

                    foreach (string tag in blogTagListBox.Items) {
                        if (tag.Equals(cat.Term)) {
                            already_taged = true;
                            break;
                        }
                    }

                    if (!already_taged) {
                       blogTagListBox.Items.Add(cat.Term);
                    } else {
                        already_taged = false;
                    }
                }
            }*/
            

            blogReaderWebBrowser.DocumentText = PloggerAtomUtil.contruct_html_doc_from_atom_feed(feed);
        }
    }
}
